//Assures that the class can be used from other other classes as a 
//component and be wired automatically by the Spring framework
@Component
//All operations done by this class have to be done completely or be 
//rolled back entirely.
@Transactional
public class CustomerDomainImpl implements CustomerDomain {

	//Use previous described class CustomerRepository automatically 
	//Let the Spring framework wire the given variable with a living 
	//repository instance in the background
    @Autowired
    CustomerRepository customerRepository;

    public void saveCustomer(Customer customer) throws CustomersHaveToHaveAtLeastFirstAndLastName, CustomersHaveToHaveAtMostFirstAndLastName {

        //Customers must have a first and a last name
        if ( customer.getName() == null 
        		|| customer.getName().split(" ").length < 2 
        		|| customer.getName().length() < 3)
            throw new CustomersHaveToHaveAtLeastFirstAndLastName();

        //Customers must not have more than one first and one last name
        if (customer.getName().split(" ").length > 2)
            throw new CustomersHaveToHaveAtMostFirstAndLastName();

        customerRepository.save(customer);
    }

    public Customer updateCustomer(Customer customer) { 
    	return customerRepository.save(customer); }    
    
    public List<Customer> findAllCustomers() { 
    	return customerRepository.findAll(); }

    public Customer findCustomer(Long id) { 
    	return customerRepository.findOne(id); }

    public void deleteCustomer(Customer customer) { 
    	customerRepository.delete(customer); }
    //Further methods here ...
}